<?php

class SortsAction extends Action{
	
	//分类列表
	public function sorts(){
		
		$Sorts = M('Sorts');
		$list=$Sorts->order('path')->select();
		$this->assign('list',$list);
		$this->display();
		
	}//列表结束
	
	//接收表单数据写入数据库
	public function sorts_insert(){
		
		//admin_filter公用函数,简过滤
		$_POST['names'] = admin_filter($_POST['names']);
			$parent=explode(',',$_POST['parent']);
			$_POST['pid']=$parent[0];
			$_POST['path']=$parent[1];

		$Sorts = M('Sorts');
		if($Sorts->create()){
			$result = $Sorts->add();
			
			if($result){
				//得到分类ID修改path字段用于排序
				$Sorts->where('id='.$result)->setField('path',$_POST['path'].'-'.$result);
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->error($Sorts->getError());
		}
			
	}//写入数据库结束
	//分类删除
	public function sorts_del(){
		$Sorts = M('Sorts');
		if($Sorts->where('path="'.$_GET['path'].'" or path like "'.$_GET['path'].'-%"')->delete()){
			$this->success('删除成功');
		}else{
			var_dump($Sorts->where('path='.$_GET['path'].' or path like "'.$_GET['path'].'%"')->delete());exit();
			$this->error($Sorts->getError());
		}
	}//删除结束
	//修改页面
	public function sorts_edi(){
		
		$Sorts = M('Sorts');
		$list=$Sorts->order('path')->select();
		$this->assign('list',$list);
		$Sorts_edi = M('Sorts');
		$Sorts_edi=$Sorts_edi->where('id='.$_GET['id'])->find();
		$this->assign('Sorts_edi',$Sorts_edi);
		$this->display('sorts');
	}
	
	public function sorts_save(){
		$_POST['names'] = admin_filter($_POST['names']);
		$parent=explode(',',$_POST['parent']);
		$_POST['pid']=$parent[0];
		$_POST['path']=$parent[1].'-'.$_POST['id'];

		$Sorts = M("Sorts"); // 实例化User对象
		if($Sorts->create()){// 根据表单提交的POST数据创建数据对象
			if($Sorts->save()){ // 				
				$this->success('修改成功','__APP__/Sorts/sorts');
			}else{
				$this->error('修改失败');
			}
			
		}else{
			$this->error($Sorts->getError());
		}
	}
	//修改页面结束
}




?>